package org.malred.hotelsystem.activities.admin;

import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.malred.hotelsystem.R;
import org.malred.hotelsystem.activities.BaseActivity;
import org.malred.hotelsystem.activities.admin.ManageUpdateActivity;
import org.malred.hotelsystem.activities.admin.ManagerActivity;
import org.malred.hotelsystem.dao.HotelDao;
import org.malred.hotelsystem.model.entity.HotelEntity;

import java.util.HashMap;
import java.util.Map;

public class HotelDetailActivity extends BaseActivity {
    private static final String TAG = "HotelDetailActivity";

    TextView tv_title;
    TextView tv_desc;
    TextView tv_num;

    Button uptBtn;
    Button delBtn;

    // 传递来的旅店id
    String hotelId;

    @Override
    protected int initLayout() {
        return R.layout.activity_hotel_detail;
    }

    @Override
    protected void initView() {
        tv_title = findViewById(R.id.detail_title);
        tv_desc = findViewById(R.id.detail_desc);
        tv_num = findViewById(R.id.detail_num);

        uptBtn = findViewById(R.id.detail_upt_btn);
        delBtn = findViewById(R.id.detail_del_btn);
    }

    @Override
    protected void initData() {
        hotelId = (String) intentExtras.get("hotel_id");
        Log.i(TAG, "initData: " + hotelId);
        HotelEntity hotel = HotelDao.getById(getDb(), hotelId);

        tv_title.setText(hotel.getTitle());
        tv_desc.setText(hotel.getDescription());
        tv_num.setText("库存: " + String.valueOf(hotel.getNumber()));

        uptBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, String> args = new HashMap();
                args.put("h_id", hotelId);
                args.put("h_title", hotel.getTitle());
                args.put("h_desc", hotel.getDescription());
                args.put("h_num", hotel.getNumber() + "");
                navigateTo(ManageUpdateActivity.class, args);
            }
        });

        delBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertClick("删除酒店信息", "是否删除?",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                HotelDao.del(getDb(), hotelId);
                                navigateTo(ManagerActivity.class);
                                dialog.cancel();
                                showToastSync("删除成功");
                            }
                        },
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                                showToastSync("取消删除");
                            }
                        });
            }
        });
    }

}
